﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_Home_Default : Acms.AcmsControls.TemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (AcmsContext.CurrentPost != null)
            {
                SetImage(link1, "Link 1", "Link Image 1");
                SetImage(link2, "Link 2", "Link Image 2");
                SetImage(link3, "Link 3", "Link Image 3");
            }
        }

    }

    private void SetImage(HyperLink link, string relatedPostName, string imageName)
    {
        var width = 272;
        var height = 75;
        var relatedPost = AcmsContext.CurrentPost.FindPostControl(relatedPostName);
        if(relatedPost != null)
        {
            var relatedPostProperties = relatedPost.GetProperties<Acms.AcmsControls.RelatedPostsContentControlProperties>();
            if(relatedPostProperties != null)
            {
                var post = relatedPostProperties.GetPosts(AcmsContext.SiteId, AcmsContext.SiteVersion).FirstOrDefault();
                if(post != null)
                {
                    link.Visible = true;
                    link.NavigateUrl = post.Url;
                    link.ToolTip = post.DisplayName;
                    link.Text = post.DisplayName;
                    if(post.ThumbnailId.HasValue)
                        link.ImageUrl = Acms.AcmsControls.RenderFile.GetUrl(post.ThumbnailId.Value, width, height, this);

                    var image = AcmsContext.CurrentPost.FindPostControl(imageName);
                    if (image != null)
                    {
                        var imageProperties = image.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                        if (imageProperties != null && imageProperties.HasResource())
                        {
                            link.ImageUrl = Acms.AcmsControls.RenderFile.GetUrl(imageProperties.ResourceId, width, height, this);
                        }
                    }
                }
            }
        }
    }
}
